package com.lysj.admin.gateway.filter;

import com.lysj.admin.utils.json.JacksonUtil;
import com.lysj.admin.utils.web.Resp;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.log4j.Log4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Created by zk on 2019-01-03 15:47
 * @description 异常拦截器
 */
@Log4j
public class ErrorFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "error";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext ctx = RequestContext.getCurrentContext();
        Throwable e = ctx.getThrowable();
        if(e.getCause() != null){
            StringBuilder sb = new StringBuilder();
            sb.append(e.getCause());
            if(e.getCause().getStackTrace()!=null && e.getCause().getStackTrace().length>0){
                sb.append("--").append(e.getCause().getStackTrace()[0]);
            }
            log.error(sb.toString());
        }
        return null;
    }
}
